package com.agh.portal.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.agh.portal.data.DataComponent;
import com.agh.portal.data.UserAction;
import com.agh.portal.data.UserNewContent;
import com.agh.portal.domain.Ad;
import com.agh.portal.domain.Answer;
import com.agh.portal.domain.Comment;
import com.agh.portal.domain.GroupEvent;
import com.agh.portal.domain.MainPagePost;
import com.agh.portal.domain.Post;
import com.agh.portal.domain.Question;
import com.agh.portal.domain.Reply;
import com.agh.portal.domain.util.ToStringUtil;

public class DataService {
	
	public static List<DataComponent> parseAdsToDataComponent(List<Ad> ads){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Ad ad : ads){
			UserNewContent content = new UserNewContent();
			content.setStudent(ad.getStudent());
			content.setComponent(ad);
			content.setPath("/ads");
			content.setTitle(ToStringUtil.cutString(ad.getTitle(), 50));
			content.setDescription(ToStringUtil.cutString(ad.getContent(), 150));
			content.setDate(ad.getCreationDate());
			content.setDomainName("Ad");
			components.add(content);
		}
		return components;
	}
	

	public static List<DataComponent> parseAnswersToDataComponent(List<Answer> answers){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Answer answer : answers){
			UserAction action = new UserAction();
			action.setStudent(answer.getStudent());
			action.setComponent(answer);
			action.setContent(ToStringUtil.cutString(answer.getContent(), 150));
			action.setTargetId(answer.getQuestion().getId().toString());
			action.setTargetPath("/questions/"+answer.getQuestion().getId());
			action.setTargetTitle(ToStringUtil.cutString(answer.getQuestion().getTitle(), 50));
			action.setTargetDomainName("Question");
			action.setDate(answer.getCreationDate());
			components.add(action);
		}
		return components;
	}
	
	public static List<DataComponent> parseCommentsToDataComponent(List<Comment> comments){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Comment comment : comments){
			UserAction action = new UserAction();
			action.setStudent(comment.getStudent());
			action.setComponent(comment);
			action.setContent(ToStringUtil.cutString(comment.getContent(), 150));
			action.setTargetId(comment.getMainPagePost().getId().toString());
			action.setTargetPath("/mainpageposts/"+comment.getMainPagePost().getId());
			action.setTargetTitle(ToStringUtil.cutString(comment.getMainPagePost().getTitle(), 50));
			action.setTargetDomainName("MainPagePost");
			action.setDate(comment.getCreationDate());
			components.add(action);
		}
		return components;
	}
	
	public static List<DataComponent> parseMainPagePostsToDataComponent(List<MainPagePost> mainPagePosts){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(MainPagePost post : mainPagePosts){
			UserNewContent content = new UserNewContent();
			content.setStudent(post.getStudent());
			content.setComponent(post);
			content.setPath("/mainpageposts");
			content.setTitle(ToStringUtil.cutString(post.getTitle(), 50));
			content.setDescription(ToStringUtil.cutString(post.getContent(), 150));
			content.setDate(post.getCreationDate());
			content.setDomainName("MainPagePost");
			components.add(content);
		}
		return components;
	}
	
	public static List<DataComponent> parsePostsToDataComponent(List<Post> posts){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Post post : posts){
			UserNewContent content = new UserNewContent();
			content.setStudent(post.getStudent());
			content.setComponent(post);
			content.setPath("/posts");
			content.setTitle(ToStringUtil.cutString(post.getTitle(), 50));
			content.setDescription(ToStringUtil.cutString(post.getContent(), 150));
			content.setDate(post.getPostDate());
			content.setDomainName("Post");
			components.add(content);
		}
		return components;
	}
	
	public static List<DataComponent> parseQuestionsToDataComponent(List<Question> questions){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Question question : questions){
			UserNewContent content = new UserNewContent();
			content.setStudent(question.getStudent());
			content.setComponent(question);
			content.setPath("/questions");
			content.setTitle(ToStringUtil.cutString(question.getTitle(), 50));
			content.setDescription(ToStringUtil.cutString(question.getContent(), 150));
			content.setDate(question.getCreationDate());
			content.setDomainName("Question");
			components.add(content);
		}
		return components;
	}
	
	public static List<DataComponent> parseRepliesToDataComponent(List<Reply> replies){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(Reply reply : replies){
			UserAction action = new UserAction();
			action.setStudent(reply.getStudent());
			action.setComponent(reply);
			action.setContent(ToStringUtil.cutString(reply.getContent(), 150));
			action.setTargetId(reply.getPost().getId().toString());
			action.setTargetPath("/posts/"+reply.getPost().getId());
			action.setTargetTitle(ToStringUtil.cutString(reply.getPost().getTitle(),50));
			action.setTargetDomainName("Post");
			action.setDate(reply.getReplyDate());
			components.add(action);
		}
		return components;
	}
	
	public static List<DataComponent> parseGroupEventsToDataComponent(List<GroupEvent> groupEvents){
		List<DataComponent> components = new ArrayList<DataComponent>();
		for(GroupEvent groupEvent : groupEvents){
			UserNewContent content = new UserNewContent();
			content.setStudent(groupEvent.getStudent());
			content.setComponent(groupEvent);
			content.setPath("/groupevents");
			content.setTitle(ToStringUtil.cutString(groupEvent.getTitle(), 50));
			content.setDescription(ToStringUtil.cutString(groupEvent.getContent(), 150));
			content.setDate(groupEvent.getEventDate());
			content.setDomainName("GroupEvent");
			components.add(content);
		}
		return components;
	}
	
	public static List<DataComponent> sortByDateAsc(List<DataComponent> items){
		Collections.sort(items, new Comparator<DataComponent>(){
			public int compare(DataComponent d1, DataComponent d2){
				return d1.getDate().compareTo(d2.getDate());
			}
		});
		return items;
	}
	
	public static List<DataComponent> sortByDateDesc(List<DataComponent> items){
		Collections.sort(items, new Comparator<DataComponent>(){
			public int compare(DataComponent d1, DataComponent d2){
				return d2.getDate().compareTo(d1.getDate());
			}
		});
		return items;
	}
	
	public static List<Comment> sortCommentsByDate(Collection<Comment> collection){
		List<Comment> sortedList = new ArrayList<Comment>(collection);
		
		Collections.sort(sortedList, new Comparator<Comment>() {
		    public int compare(Comment c1, Comment c2) {
		        return c1.getCreationDate().compareTo(c2.getCreationDate());
		    }
		});
		
		return sortedList;
	}
	
	public static List<Post> sortPostsByDate(Collection<Post> collection){
		List<Post> sortedList = new ArrayList<Post>(collection);
		
		Collections.sort(sortedList, new Comparator<Post>() {
		    public int compare(Post o1, Post o2) {
		        return o1.getPostDate().compareTo(o2.getPostDate());
		    }
		});
		
		return sortedList;
	}
	
	public static List<Answer> sortAnswersByDate(Collection<Answer> collection){
		List<Answer> sortedList = new ArrayList<Answer>(collection);
		
		Collections.sort(sortedList, new Comparator<Answer>() {
		    public int compare(Answer o1, Answer o2) {
		        return o1.getCreationDate().compareTo(o2.getCreationDate());
		    }
		});
		
		return sortedList;
	}
	
	public static List<Reply> sortRepliesByDate(Collection<Reply> collection){
		List<Reply> sortedList = new ArrayList<Reply>(collection);
		
		Collections.sort(sortedList, new Comparator<Reply>() {
		    public int compare(Reply o1, Reply o2) {
		        return o1.getReplyDate().compareTo(o2.getReplyDate());
		    }
		});
		
		return sortedList;
	}
	
}
